/*
 * @Author: your name
 * @Date: 2021-01-28 15:12:15
 * @LastEditTime: 2021-03-31 21:34:42
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tower_defense\assets\scripts\game_scene\tower_menu.ts
 */

const {ccclass, property} = cc._decorator;

@ccclass
export default class TowerMenu extends cc.Component {

    @property(cc.Node)  // 塔建造界面
    public tower_build_node: cc.Node = null;
    @property(cc.Node)  // 塔升级界面
    public tower_upgrade_node: cc.Node = null;

    public tower_builder = null;

    onLoad() {
        this.node.active = false;
        this.tower_builder = null;
    }

    // 打开塔建造界面
    public show_tower_build_panel(tower_builder): void {
        this.tower_builder = tower_builder;
        if (!this.tower_builder) {
            return;
        }

        this.node.active = true;
        this.tower_build_node.active = true;
        this.tower_upgrade_node.active = false;
        
        this.node.x = tower_builder.node.x;
        this.node.y = tower_builder.node.y;
    }

    // 打开塔升级界面
    public show_tower_upgrade_panel(tower_builder): void {
        this.tower_builder = tower_builder;
        if (!this.tower_builder) {
            return;
        }

        this.node.active = true;
        this.tower_upgrade_node.active = true;
        this.tower_build_node.active = false;

        this.node.x = tower_builder.node.x;
        this.node.y = tower_builder.node.y;
    }

    // 建塔点击事件
    public on_build_tower_click(e, tower_type: number): void {
        if (!this.tower_builder) {
            return;
        }
        this.tower_builder.on_tower_build_click(e, tower_type);
        this.close_tower_menu();
    }

    // 塔回收点击事件
    public on_sell_tower_click(): void {
        if (!this.tower_builder) {
            return;
        }
        this.tower_builder.on_tower_sell_click();
        this.close_tower_menu();
    }

    // 塔升级点击事件
    public on_upgrade_tower_click(): void {
        if (!this.tower_builder) {
            return; ``
        }
        this.tower_builder.on_tower_upgrade_click();
        this.close_tower_menu();
    }

    // 关闭建塔菜单界面
    public close_tower_menu(): void {
        this.node.active = false;
    }
}
